//
//  GoodsCommentController.m
//  WuJieLive
//
//  Created by iMac on 2018/1/10.
//  Copyright © 2018年 zhk. All rights reserved.
//

#import "GoodsCommentController.h"
#import "HCSStarRatingView.h"
#import "CommentModel.h"
#import "CommentsCell.h"
#import "HDSegmentMenu.h"

@interface GoodsCommentController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic , strong) UIImageView * imageView;
@property (nonatomic , strong) HCSStarRatingView * starView;
@property (nonatomic , strong) UILabel * label;

@property (nonatomic , assign) NSInteger index;
@property (nonatomic , strong) NSMutableArray * dataArray;
@property (nonatomic , strong) UITableView * tableview;

@property (nonatomic , strong) NSString * hasImage;

@property (nonatomic , strong) HDSegmentMenu * menu;

@end

@implementation GoodsCommentController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
}

-(void)setNavi
{
    self.title = @"全部评价";
}

-(void)setUI
{
    self.view.backgroundColor = Color(245, 245, 245);
    UIView * view = [[UIView alloc]init];
    view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:view];
    [view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.top.offset(0);
        make.height.offset(75);
    }];
    self.imageView = [[UIImageView alloc]init];
    [view addSubview:self.imageView];
    [self.imageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.offset(15);
        make.centerY.equalTo(view);
        make.size.mas_offset(CGSizeMake(45, 45));
    }];
    self.imageView.layer.masksToBounds = YES;
    self.imageView.layer.cornerRadius = 2;
    
    self.starView = [[HCSStarRatingView alloc] init];
    [view addSubview:self.starView];
    self.starView.maximumValue = 5;
    self.starView.minimumValue = 0;
    self.starView.tintColor = Color(255, 203, 101);
    [self.starView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(self.imageView.mas_right).offset(15);
        make.centerY.equalTo(view);
        make.size.mas_offset(CGSizeMake(100, 20));
    }];
    
    self.label = [[UILabel alloc]init];
    [view addSubview:self.label];
    self.label.textColor = TextAssistColor;
    self.label.font = FontSize(15);
    [self.label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.offset(-15);
        make.centerY.equalTo(view);
    }];
    
    self.menu = [[HDSegmentMenu alloc]initWithFrame:CGRectMake(0, 85, kScreenWidth, 40) titlesArray:@[@"全部评价",@"只看有图"] selectedColor:MainColor itemSpace:20 withSelectedIndex:0];
    [self.view addSubview:self.menu];
    __weak typeof(self) weakSelf = self;
    self.menu.segmentSelectedBlock = ^(NSInteger index) {
        weakSelf.hasImage = [NSString stringWithFormat:@"%ld",index];
        [weakSelf requestData];
    };
    
    self.tableview = [[UITableView alloc]initWithFrame:CGRectMake(0, 125, kScreenWidth, kScreenHeight - 125) style:(UITableViewStylePlain)];
    self.tableview.delegate = self;
    self.tableview.dataSource = self;
    [self.view addSubview:self.tableview];
    self.tableview.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.tableview registerNib:[UINib nibWithNibName:@"CommentsCell" bundle:nil] forCellReuseIdentifier:@"cell"];
    self.tableview.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(requestData)];
    self.tableview.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(requestMoreData)];
    [self.tableview.mj_header beginRefreshing];
}

-(void)setData
{
    self.hasImage = @"0";
}

- (void)requestData
{
    self.index = 1;
    [self requestMoreData];
}

- (void)requestMoreData
{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSDictionary * dict = @{@"goods_id":self.goodsID,@"has_picture":self.hasImage,@"page":[NSString stringWithFormat:@"%ld",self.index]};
    [HTTPTool POSTForm:[NSString stringWithFormat:@"%@goods/goods-comment",BaseUrl] para:dict success:^(BOOL isSuccess, id responseObject) {
        [MBProgressHUD hideHUDForView:self.view animated:YES];
        if (isSuccess) {
            if (self.index == 1) {
                self.dataArray = [NSMutableArray array];
            }
            [self.imageView sd_setImageWithURL:[NSURL URLWithString:responseObject[@"data"][@"goods_picture"]] placeholderImage:PlaceImage];
            self.starView.value = [responseObject[@"data"][@"comment_count"] floatValue];
            self.label.text = [NSString stringWithFormat:@"%@好评",responseObject[@"data"][@"comment_percentage"]];
            for (int i = 0; i < [responseObject[@"data"][@"comment_content"] count]; i ++) {
                CommentModel * model = [[CommentModel alloc]init];
                [model setValuesForKeysWithDictionary:responseObject[@"data"][@"comment_content"][i]];
                [self.dataArray addObject:model];
            }
            if ([responseObject[@"hasmore"] integerValue] == 0) {
                [self.tableview.mj_footer endRefreshingWithNoMoreData];
            }
            self.index += 1;
            [self.tableview reloadData];
            [self endRefresh];
        }
    } failure:^(NSError *error) {
        [self endRefresh];
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    }];
}

- (void)endRefresh
{
    if (self.tableview.mj_header.isRefreshing) {
        [self.tableview.mj_header endRefreshing];
    }else
    {
        [self.tableview.mj_footer endRefreshing];
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentModel * model  = self.dataArray[indexPath.row];
    if (model.pictures.count > 0) {
        return 130 - 16 + model.textHeight + 100;
    }
    return 130 - 16 + model.textHeight;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataArray.count;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CommentsCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cell" forIndexPath:indexPath];
    CommentModel * model  = self.dataArray[indexPath.row];
    [cell bindDataWithModel:model];
    return cell;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
